using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Input.Touch;
using Microsoft.Xna.Framework.Media;

namespace Pyramid_Game
{
    public class KeyFind : GameScreen
    {
        #region Declarations
        public Keys[] myKeys;
        
        float[] delta = new float[3];
        int selectedsaw = -1;
        #endregion

        #region Load

        public KeyFind()
        {
            myKeys = new Keys[3];
            
        }
        public override void LoadContent(ContentManager content)
        {
            Texture2D saw;
            saw = content.Load<Texture2D>("Texture/Keys/saw_1");
            int width = 190;
            int height = 191;
            myKeys[0] = new Keys(saw, Game1.viewport.Width / 2, Game1.viewport.Height / 2 , width, height, 0.1f);


            saw = content.Load<Texture2D>("Texture/Keys/saw_2");
            width = 381;
            height = 382;
            myKeys[1] = new Keys(saw, Game1.viewport.Width / 2, Game1.viewport.Height / 2 , width, height, 0.2f);

            saw = content.Load<Texture2D>("Texture/Keys/saw_3");
            width = 476;
            height = 477;
            myKeys[2] = new Keys(saw, Game1.viewport.Width / 2 , Game1.viewport.Height / 2 , width, height, 0.4f);

        }

        #endregion

        #region Update & Draw

        public override void Update(Touch touch, GameTime gameTime)
        {
            bool b_clear = true;
            foreach (Keys k in myKeys)
            {
                b_clear = b_clear & k.Check_degree();
            }
            if (b_clear == true)
            {
                screenManager.AddScreen(new Clear(1));
                ScreenManager.RemoveScreen(this);
            }
            HandleInput(touch, gameTime);
        }

        public void HandleInput(Touch touch, GameTime gameTime)
        {
            delta[0] = 0f;
            delta[1] = 0f;
            delta[2] = 0f;

            foreach (GestureSample gesture in touch.Gestures)
            {
                if (gesture.GestureType == GestureType.Tap)
                {
                    for(int i = 0; i<3; i++)
                    {
                        
                        if(myKeys[i].Location().Contains((int)gesture.Position.X, (int)gesture.Position.Y))
                        {
                            if (!myKeys[i].IsRegionTransparent(
                                new Rectangle(Math.Abs((int)gesture.Position.X - myKeys[i].Location().X), Math.Abs((int)gesture.Position.Y - myKeys[i].Location().Y), 1, 1)))
                            {
                                if (selectedsaw != -1)
                                    myKeys[selectedsaw].Change_Gamma();
                                selectedsaw = i;
                                myKeys[i].Change_Gamma();
                                break;
                            }
                            
                        }
                    }
                    
                }
                if (selectedsaw != -1 &&gesture.GestureType == GestureType.FreeDrag)
                {

                        if (Math.Abs(gesture.Delta.X) > 0)
                        {
                            delta[selectedsaw] = gesture.Delta.X;
                        }

                    myKeys[selectedsaw].Update(delta[selectedsaw]);
                    
                }
                
            }
        }
        public override void Draw(SpriteBatch spriteBatch, GameTime gameTime)
        {

            spriteBatch.End();
            spriteBatch.Begin(SpriteSortMode.BackToFront, BlendState.AlphaBlend);
            foreach (Keys k in myKeys)
            {
                k.Draw(spriteBatch);
            }
            spriteBatch.End();
            spriteBatch.Begin();
            
        }

        #endregion

        #region HelperMethods

        #endregion
    }
}
